package com.java110.mall.acct.integral.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.integral.DeductionIntegralDto;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.IIntegralSettingV1InnerServiceSMO;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.acct.integral.IComputeDeductionIntegral;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ComputeDeductionIntegralImpl implements IComputeDeductionIntegral {


    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralSettingV1InnerServiceSMO integralSettingV1InnerServiceSMOImpl;

    @Override
    public DeductionIntegralDto deduction(String userId, String orderId, String shopId) {


        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);

        if (ListUtil.isNull(userDtos)) {
            return new DeductionIntegralDto(0, 0);
        }

        IntegralDto integralDto = new IntegralDto();
        integralDto.setLink(userDtos.get(0).getTel());
        integralDto.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);
        JSONObject paramOut = new JSONObject();
        int integral = 0;
        if (!ListUtil.isNull(integralDtos)) {
            integral = Integer.parseInt(integralDtos.get(0).getAmount());
        }

        //todo 计算成金额
        double money = integralSettingV1InnerServiceSMOImpl.computeMoney(integral);


        DeductionIntegralDto deductionIntegralDto = new DeductionIntegralDto(integral, money);
        deductionIntegralDto.setLink(userDtos.get(0).getTel());
        deductionIntegralDto.setShopId(shopId);
        CommonCache.setValue("integral_deduction_" + orderId, JSONObject.toJSONString(deductionIntegralDto), CommonCache.PAY_DEFAULT_EXPIRE_TIME);
        return deductionIntegralDto;
    }
}
